package com.njzxw.fs.commonutil.spring;

import com.njzxw.fs.commonutil.util.LoadSpringBean;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

public class ApplicationInit implements ApplicationContextInitializer {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        reload(applicationContext);
    }

    public void reload(ConfigurableApplicationContext context){

        String lib = System.getProperty("user.dir")+ File.separator+"lib";
        File file = new File(lib);

        if(!file.exists()){
            file.mkdirs();
        }

//        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();
        ClassLoader beanClassLoader = context.getBeanFactory().getBeanClassLoader();
        List<Class<?>> clazzs = new LoadSpringBean().loadClass(lib,beanClassLoader);
        for(Class<?> clazz : clazzs){
            if(!clazz.isInterface()){
//                if(clazz.getAnnotation(Controller.class) != null ||
//                        clazz.getAnnotation(RestController.class) != null ||
//                        clazz.getAnnotation(Service.class) != null ||
//                        clazz.getAnnotation(Component.class) != null){
//                    BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.genericBeanDefinition(clazz);
//                    context.getBeanFactory().registerDependentBean();
//                    context.getBeanFactory().registerBeanDefinition(clazz.getSimpleName(),beanDefinitionBuilder.getRawBeanDefinition());
//                }

            }
        }

    }

}
